<template>
  <el-form ref="recordRefForm" :model="recordQuery" label-width="110px" :rules="recordRules">
    <el-form-item label="记录名称" prop="name">
      <el-input v-model="recordQuery.name" />
    </el-form-item>

    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
          v-model="recordQuery.startTime"
          type="datetime"
          placeholder="请选择创建时间"
          value-format="YYYY-MM-DD HH:mm:ss" style="width:100%;"/>
    </el-form-item>

    <el-form-item label="结束时间"  prop="endTime">
      <el-date-picker
          v-model="recordQuery.endTime"
          type="datetime"
          placeholder="请选择创建时间"
          value-format="YYYY-MM-DD HH:mm:ss" style="width:100%;"/>
    </el-form-item>

    <el-form-item label="记录描述" prop="description">
      <el-input
          v-model="recordQuery.description"
          :rows="6"
          type="textarea"
          placeholder="请输入记录描述"/>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="recordSubmit">提 交</el-button>
      <el-button @click="goBack">返 回</el-button>
    </el-form-item>

  </el-form>
</template>

<script>
import {defineComponent} from 'vue'
import {doGet, doPost, doPut} from "../http/httpRequest.js";
import {messageTip} from "../util/util.js";

export default defineComponent({
  name: "RecordInputView",

  //注入
  inject : ['reload'],

  data() {
    return {
      //员工记录表单对象，初始值是空
      recordQuery : {},
      //记录表单验证规则
      recordRules : {
        name : [
          { required: true, message: '请输入记录名称', trigger: 'blur' }
        ],
        startTime : [
          { required: true, message: '请选择开始时间', trigger: 'blur' }
        ],
        endTime : [
          { required: true, message: '请选择结束时间', trigger: 'blur' }
        ],
        description : [
          { required: true, message: '请输入记录描述', trigger: 'blur' },
          { min: 5, max: 255, message: '记录描述长度为5-255个字符', trigger: 'blur' }
        ]
      },
    }
  },

  mounted() {
    //加载要编辑的数据（由于录入和编辑共用一个页面，所以要判断是 编辑还是录入）
    this.loadRecord();
  },

  methods : {
    //返回
    goBack() {
      this.$router.go(-1);
    },

    //员工记录提交
    recordSubmit()  {
      let formData = new FormData();
      for (let field in this.recordQuery) {
        console.log(field  + " -- " + this.recordQuery[field])
        if (this.recordQuery[field])  { //this.recordQuery[field]有值，this.recordQuery[field]不是空，this.recordQuery[field]存在
          formData.append(field, this.recordQuery[field]);
        }
      }
      this.$refs.recordRefForm.validate( (isValid) => {
        if (isValid) {
          if (this.recordQuery.id > 0) { /*编辑*/
            doPut("/api/record", formData).then(resp => {
              if (resp.data.code === 200) {
                messageTip("编辑成功", "success");
                //跳转到员工记录列表页
                this.$router.push("/dashboard/record");
              } else {
                messageTip("编辑失败", "error");
              }
            })
          } else  {
            doPost("/api/record", formData).then(resp => {/*新增*/
              if (resp.data.code === 200) {
                messageTip("提交成功", "success");
                //跳转到员工记录列表页
                this.$router.push("/dashboard/record");
              } else {
                messageTip("提交失败", "error");
              }
            })
          }
        }
      })
    },

    //加载要编辑的员工记录数据
    loadRecord() {
      let id =  this.$route.params.id;
      if (id) { //id存在，id有值，id不为空，说明的编辑
        doGet("/api/record/" + id, {}).then(resp => {
          if (resp.data.code === 200) {
            this.recordQuery = resp.data.data;
          }
        })
      }
    }
  }
})
</script>

<style scoped>
.width {
  width: 100%;
}
</style>
